﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Reflection;
using System.Text;

namespace Lifang.Dao
{
    public class DALUnitity
    {

        public static string FormatSql(string sql, object param)
        {
            if (param == null)
                return sql;

            Type type = param.GetType();
            if (!type.IsClass)
                return sql;

            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                return FormatSqlWithBatchParameters(sql, param as IEnumerable);
            }
            else
            {
                PropertyInfo[] propertyInfos = type.GetProperties();
                foreach (var propertyInfo in propertyInfos)
                {
                    Type valueType = propertyInfo.PropertyType;
                    if (valueType.IsGenericType && valueType.FullName.StartsWith("System.Nullable"))
                    {
                        valueType = valueType.GenericTypeArguments[0];
                    }

                    string strValue = "''";
                    object v = propertyInfo.GetValue(param);
                    if (v != null)
                    {
                        if (valueType == typeof(string))
                        {
                            strValue = string.Format("'{0}'", v);
                        }
                        else if (valueType == typeof(DateTime))
                        {
                            strValue = string.Format("'{0}'", ((DateTime)v).ToString("yyyy-MM-dd HH:mm:ss.fff"));
                        }
                        else if (valueType.IsEnum)
                        {
                            strValue = string.Format("{0}", (v as System.Enum).ToString("d"));
                        }
                        else
                        {
                            strValue = string.Format("{0}", v);
                        }
                    }
                    sql = sql.Replace(string.Format("@{0}", propertyInfo.Name), strValue);
                }
                return sql;
            }
        }

        private static string FormatSqlWithBatchParameters(string sql, IEnumerable parameters)
        {
            if (parameters == null)
                return sql;

            StringBuilder sb = new StringBuilder();
            foreach (var param in parameters)
            {
                sb.AppendLine(FormatSql(sql, param));
            }
            return sb.ToString();
        }


        public static string GetsqlWhere(NameValueCollection collection)
        {
            List<string> listWhere = new List<string>();
            if (collection.Count > 0)
            {
                foreach (var key in collection.AllKeys)
                {
                    if (key.EndsWith(">")
                        || key.EndsWith(">=")
                        || key.EndsWith("<")
                        || key.EndsWith("!=")
                        || key.EndsWith("%"))
                    {
                        listWhere.Add(string.Format(" {0} '{1}' ", key, collection[key]));
                    }
                    else if (key.EndsWith("in"))
                    {
                        listWhere.Add(string.Format(" {0} ({1}) ", key, collection[key]));
                    }
                    else
                    {
                        listWhere.Add(string.Format(" {0}='{1}' ", key, collection[key]));
                    }
                }
            }
            return string.Join(" AND ", listWhere);
        }
    }
}
